package api;



import bean.sendTemplate.SendItem;
import bean.sendTemplate.SendTemplate;
import com.alibaba.fastjson.JSON;
import common.utils.DateUtils;
import common.utils.MyMapUtils;
import httpclient.HttpClientUtils;
import httpclient.bean.HttpResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.cfg.WxCfg;
import utils.JsonUtil;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;


/**
 * 模版消息 api
 */
@Service
public class TemplateMsgAPI {
    private static final Logger logger = LoggerFactory.getLogger(TemplateMsgAPI.class);

    private static final String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";

    private static final String sendUrl = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=%s";

    @Autowired
    WxCfg wxCfg;

    public String getAccessToken() {
        String url = String.format(tokenUrl, wxCfg.getApp_id(), wxCfg.getApp_secret());
        String result = HttpClientUtils.getBodyStr(url);
        logger.warn("---getAccessToken--" + result);
        Map maps = (Map) JSON.parse(result);
        return maps.get("access_token").toString();
    }

    public void sendTemplateMsg(String openId,String formid){
        SendTemplate template = new SendTemplate();
        String token=this.getAccessToken();
        template.setEmphasis_keyword("");
        template.setForm_id(formid);
        template.setPage("pages/businessQrcode/businessQrcode");
        template.setTemplate_id("Q3AhSRwLpQDeP0crbhA3ROZ2NgGy2HUhfzKyTbcpOfQ");
        template.setTouser(openId);
        Map<String,SendItem> itemMap = new TreeMap<>();
        itemMap.put("keyword5",new SendItem("店主须知:请点击下方查看详情开启店铺雷达功能"));
        itemMap.put("keyword4",new SendItem(DateUtils.getDateTime()));
        itemMap.put("keyword3",new SendItem(DateUtils.formatDateTime(DateUtils.getAddDay(new Date(),365))));
        itemMap.put("keyword2",new SendItem("微脉脉店铺店主"));
        itemMap.put("keyword1",new SendItem("店铺"));
        template.setData(itemMap);
        HttpResult result = HttpClientUtils.postW(String.format(sendUrl,token), JsonUtil.toJson(template));
        logger.warn("-sendTemplateMsg-");
        logger.warn(result.getResult().toString());

    }

    public void sendTemplateLeavingMsg(String openId,String formid,String shopName,String message,Long shopId,Long customId){
        logger.warn("--openId--openId--"+openId);
        logger.warn("--formid--formid--"+formid);
        logger.warn("--shopName--shopName--"+shopName);
        SendTemplate template = new SendTemplate();
        String token=this.getAccessToken();
        template.setEmphasis_keyword("");
        template.setForm_id(formid);
        String path="pages/chat/chat?"+"isShop=false"+"&shopName="+shopName+"&customId="+customId+"&shopId="+shopId;
        template.setPage(path);
        template.setTemplate_id("3Zlmh5d-KSzYuB1CAOt9E7n4uUzsrAZTNP9tzkKLPPM");
        template.setTouser(openId);
        Map<String,SendItem> itemMap = new TreeMap<>();
        itemMap.put("keyword3",new SendItem(DateUtils.getDateTime()));
        itemMap.put("keyword2",new SendItem(message));
        itemMap.put("keyword1",new SendItem(shopName));
        template.setData(itemMap);
        logger.warn("-sendTemplateLeavingMsg-");
        HttpResult result = HttpClientUtils.postW(String.format(sendUrl,token), JsonUtil.toJson(template));
        logger.warn("-sendTemplateLeavingMsg-");
        logger.warn(result.getResult().toString());

    }
    public static void main(String[] sta){
    //  new  TemplateMsgAPI().sendTemplateLeavingMsg("oPQnc4hfKj3Mynqmt9ad1yI3HaDI","1548670743191");
    }

}
